﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Data.Sql;
using System.Data.SqlClient;
using CNCSoft.EWinform.ERPApps;
using CNCSoft.EData;

using System.Threading;
using System.Globalization;

using Common;

namespace CNCSoft.ErpApps.TMS.Accountant
{
    public partial class frmDangKyDichVu : Form
    {
        DataTable dtDangKy;

        public frmDangKyDichVu()
        {
            // Sets the UI culture to VietNamese (VietNam) / English
            if (GlobalVariable.WSInfo.Language == "Tiếng Việt")
                Thread.CurrentThread.CurrentUICulture = new CultureInfo("vi-VN");
            else if (GlobalVariable.WSInfo.Language == "English")
                Thread.CurrentThread.CurrentUICulture = new CultureInfo("en");

            InitializeComponent();
        }

        private void txtMaNienHoc_TextChanged(object sender, EventArgs e)
        {
            try
            {
                if (txtMaNienHoc.Text == "") return;

                DataTable dt = new DataTable();
                SQLHelper.Fill(dt, GlobalVariable.WSInfo.ConnectionString, "SELECT * FROM DM_LOP WHERE MaNienHoc = '" + txtMaNienHoc.Text + "' Order by MaKhoi, STT");

                cboLop.DataSource = dt;
                cboLop.DisplayMember = "TenLop";
                cboLop.ValueMember = "MaLop";
            }
            catch (Exception ex)
            { MessageBox.Show(ex.Message); }
        }

        private void cboLop_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (cboLop.SelectedIndex == -1) return;
                DataTable dtLop = new DataTable();
                SQLHelper.Fill(dtLop, GlobalVariable.WSInfo.ConnectionString, "SELECT MaHocTap, HOCSINH_LOP.MaHocSinh, MaLop, HoLot + ' ' + Ten As HoTen, MaCheDo, MaTrangThai, STT_SoCai, getdate() as NgayVaoLop, '' as MaLopCu " +
                                                   "FROM HOCSINH_LOP " +
                                                   "JOIN HOCSINH ON HOCSINH_LOP.MaHocSinh = HOCSINH.MaHocSinh  " +
                                                   "WHERE MaLop = '" + cboLop.SelectedValue + "' Order by STT_SoCai, Ten, HoLot ");
                if (dtLop != null)
                {
                    lstHocSinh.DataSource = dtLop  ;
                    lstHocSinh.DisplayMember = "HoTen";
                    lstHocSinh.ValueMember = "MaHocTap";
                }
            }
            catch {}
            
        }

        private void lstHocSinh_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lstHocSinh.SelectedIndex > -1)
            {
                lblHoTenHocSinh.Text = lstHocSinh.Text.ToUpper();
                txtMaCheDo.Text = ((DataTable)lstHocSinh.DataSource).Rows[lstHocSinh.SelectedIndex]["MaCheDo"].ToString();
                GetData(lstHocSinh.SelectedValue.ToString());
            }
        }


        private void GetData(string strMaHocTap)
        {
            if (strMaHocTap == "" || strMaHocTap == null || strMaHocTap == "System.Data.DataRowView") return;
            try
            {
                string SqlSELECTData = " SELECT ID, '" + strMaHocTap + "' as MaHocTap, DM_DANGKYHOCPHI.SoTaiKhoan, NoiDung, ISNULL(SoLuong, SoLuongChuan) as SoLuong, GhiChu " +
                                        "FROM DM_DANGKYHOCPHI " +
                                        "LEFT JOIN (SELECT ID, MaHocTap, SoTaiKhoan, SoLuong, GhiChu FROM HOCSINH_DANGKYHOCPHI WHERE MaHocTap ='" + strMaHocTap + "') as DaDangKy  " +
                                        "ON DM_DANGKYHOCPHI.SoTaiKhoan = DaDangKy.SoTaiKhoan";
                dtDangKy = new DataTable();
                SQLHelper.Fill(dtDangKy, GlobalVariable.WSInfo.ConnectionString, SqlSELECTData);

                if (dtDangKy != null)
                    gridSoLuongDangKy.DataSource = dtDangKy;

                //SetEditMode(false);
                gridSoLuongDangKy.Focus();


            }
            catch (SqlException ex)
            {
                MessageBox.Show("Error: \n" + ex.Message);
            }
        }

        private void DuyetRowState()
        {
            if (dtDangKy == null || dtDangKy.Rows.Count == 0)
                return;
            try
            {
                //dataTable.AcceptChanges();

                foreach (DataRow row in dtDangKy.Rows)
                    if (row["ID"] == DBNull.Value)
                    {
                        row.AcceptChanges();
                        row.SetAdded();
                    }
            }
            catch { }
        }

        private void cmdCapNhat_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection cnn = new SqlConnection(GlobalVariable.WSInfo.ConnectionString);

                string sqlInsert, sqlUpdate, sqlDelete;
                sqlInsert = "INSERT INTO HOCSINH_DANGKYHOCPHI(MaHocTap, SoTaiKhoan, SoLuong, GhiChu) " +
                                              " VALUES(@pMaHocTap, @pSoTaiKhoan, @pSoLuong, @pGhiChu) ";
                sqlUpdate = "UPDATE HOCSINH_DANGKYHOCPHI SET MaHocTap=@pMaHocTap, SoTaiKhoan=@pSoTaiKhoan, SoLuong=@pSoLuong, GhiChu=@pGhiChu " +
                                   " WHERE ID=@pID";

                sqlDelete = "DELETE FROM HOCSINH_DANGKYHOCPHI WHERE ID=@pID";

                SqlParameter[] pInsert = new SqlParameter[4];
                SqlParameter[] pUpdate = new SqlParameter[5];
                SqlParameter[] pDelete = new SqlParameter[1];

                pInsert[0] = new SqlParameter("@pMaHocTap", SqlDbType.VarChar, 50, "MaHocTap");
                pInsert[1] = new SqlParameter("@pSoTaiKhoan", SqlDbType.VarChar, 25, "SoTaiKhoan");
                pInsert[2] = new SqlParameter("@pSoLuong", SqlDbType.Money, 18, "SoLuong");
                pInsert[3] = new SqlParameter("@pGhiChu", SqlDbType.NVarChar, 50, "GhiChu");

                pUpdate[0] = new SqlParameter("@pMaHocTap", SqlDbType.VarChar, 50, "MaHocTap");
                pUpdate[1] = new SqlParameter("@pSoTaiKhoan", SqlDbType.VarChar, 25, "SoTaiKhoan");
                pUpdate[2] = new SqlParameter("@pSoLuong", SqlDbType.Money, 18, "SoLuong");
                pUpdate[3] = new SqlParameter("@pGhiChu", SqlDbType.NVarChar, 50, "GhiChu");
                pUpdate[4] = new SqlParameter("@pID", SqlDbType.Money, 18, "ID"); 
                
                
                pDelete[0] = new SqlParameter("@pID", SqlDbType.VarChar, 50, "ID");

                SqlCommand cmdInsert = new SqlCommand(sqlInsert, cnn);
                SqlCommand cmdUpdate = new SqlCommand(sqlUpdate, cnn);
                SqlCommand cmdDelete = new SqlCommand(sqlDelete, cnn);

                cmdInsert.Parameters.AddRange(pInsert);
                cmdUpdate.Parameters.AddRange(pUpdate);
                cmdDelete.Parameters.AddRange(pDelete);

                SqlDataAdapter da = new SqlDataAdapter();
                da.InsertCommand = cmdInsert;
                da.UpdateCommand = cmdUpdate;
                da.DeleteCommand = cmdDelete;

                DuyetRowState();
                try
                {
                    da.Update(dtDangKy);
                    dtDangKy.AcceptChanges();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }

                //Load lai
                GetData(lstHocSinh.SelectedValue.ToString());

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void frmDangKyDichVu_Load(object sender, EventArgs e)
        {
            txtMaNienHoc.Text = GlobalVariable.WSInfo.getAppProperty("MaNienHocMoi");
        }

        private void cmdBoQua_Click(object sender, EventArgs e)
        {
            //Load lai
            GetData(lstHocSinh.SelectedValue.ToString());
        }



    }
}
